<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 18/4/3
 * Time: 14:51
 */

class GroupModel
{
    public function listsAction($user)
    {
        $data['lists'] = Db::name('group')
            ->field('id as gid,name,image,status')
            ->where(['fid'=>$user['fid']])
            ->select();
        return $data;
    }

    public function grouplistsAction($user,$content,$number=PAGE_NUMBER)
    {
        if(!empty($content['gid'])){
            $where['ll.gid'] = $content['gid'];
        }
        if(!empty($content['status'])){
            $where['ll.status'] = ['in',$content['status']];
        }else{
            $where['ll.status'] = ['in','1,2,3,5,7,8,9'];
        }
        $where['ll.fid'] = $user['fid'];
        $data['lists'] = Db::name('live_list')
            ->field('
            ll.id as llid,
            ll.name,
            ll.udid,
            ll.date,
            left(ll.start_time,5) as start_time,
            left(ll.end_time,5) as end_time,
            ll.password,
            ll.enrol,
            ll.click,
            ll.viewing,
            ll.status,
            left(ifnull(v.play_time, "0000-00-00 00:00:00"),10) as play_time,
            left(ifnull(v.play, "00:00:00"),5) as play,
            ud.nickname,
            ifnull(e.status, 2) as booked,
            ifnull(b.image,"") as bimage,
            ifnull(v.image,"") as limage,
            ifnull(v.turn, 2) as turn
            ')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->join('enroll e','ll.id = e.follow AND e.udid = '.$user['id'],'LEFT')
            ->join('backdrop b','ll.bid = b.id','LEFT')
            ->join('video v','ll.vid = v.id','LEFT')
            ->where($where)
            ->page($content['page'],$number)
            ->order('turn asc,ll.date desc')
            ->select();
        $data['page'] = $content['page'];
        $data['count'] = Db::name('live_list')->alias('ll')
            ->join('video v','ll.vid = v.id','LEFT')
            ->where($where)->count();
        return $data;
    }

    public function addAction($content,$user)
    {
        $data['udid']=$user['id'];
        $data['fid']=$user['fid'];
        $data['name'] = $content['name'];
        $data['status'] = $content['status'];
        if(!empty($content['path']) && !empty($content['file'])){
            $model = new ModelModel;
            $data['image'] = $model->moveImg($content['path'], $content['file'], 4);
        }
        Db::name('group')->insert($data);
        $msg['message'] = '分组创建成功';
        return $msg;
    }

    public function editAction($content)
    {
        if(!empty($content['path']) && !empty($content['file'])){
            $image = Db::name('group')->field('image')->where(['id'=>$content['gid']])->find();
            $imgUrl = parse_url($image['image']);
            $model = new ModelModel;
            $model->deleteImg($imgUrl['path']);
            $data['image'] = $model->moveImg($content['path'], $content['file'], 4);
        }
        $data['name'] = $content['name'];
        $data['status'] = $content['status'];
        Db::name('group')->where(['id'=>$content['gid']])->update($data);
        $msg['message'] = '分组修改成功';
        return $msg;
    }
}